package com.controller;

import com.alibaba.fastjson.JSON;
import com.bean.Post;
import com.bean.PostMessage;
import com.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : PostController
 * @Auther : acerolan@foxmail.com
 * @Data : 2024/9/24 16:59
 * @Description :
 */
@RestController
public class PostController {

    @Autowired
    PostService postService;

    @GetMapping("/post/getPosts")
    public List<Map<String, Object>> getPostsByUserId(@RequestParam("userId") int userId) {
        List<Map<String, Object>> postsList = postService.getPostsByUserId(userId);

        return postsList;
    }

    /**
     * 发布帖子
     * @param inputMap
     * @return
     */
    @PostMapping("/post/insertPost")
    public Map<String, Object> uploadGood(@RequestBody Map<String, Object> inputMap) {
        Object postObj = inputMap.get("post");
        // obj转map
        String postStr = JSON.toJSONString(postObj);
        Map<String, Object> post = JSON.parseObject(postStr);
        System.out.println(post);

        // 设置post对象属性
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String formattedDate = formatter.format(date);
        Post postBean = new Post();
        postBean.setDate(formattedDate);
        postBean.setUserId(Integer.parseInt(post.get("userId") + ""));
        postBean.setText(post.get("text") + "");

        int rt = postService.insertPost(postBean);
        Map<String, Object> outputMap = new HashMap<>();
        if (rt == 0) { // 设置异常
            outputMap.put("code", 500);
            outputMap.put("msg", "帖子发布失败，请稍后重试！");
            outputMap.put("data", null);
        } else {
            // 获取用户最新发布的帖子的id
            List<Post> postsList = postService.getPostInfoByUserId(postBean.getUserId());
            Integer postId = postsList.get(postsList.size() - 1).getId();
            Map<String, Object> data = new HashMap<>();
            data.put("postId", postId);

            outputMap.put("code", 200);
            outputMap.put("msg", "帖子发布成功！");
            outputMap.put("data", data);
        }

        return outputMap;
    }

    /**
     * 获取帖子信息
     * @param postId
     * @return
     */
    @GetMapping("/post/getPostInfo")
    public Map<String, Object> getGoodInfoByGoodId(@RequestParam("postId") int postId) {
        Map<String, Object> postInfo = postService.getPostInfoByPostId(postId);

        return postInfo;
    }

    /**
     * 帖子发送消息
     * @param goodMessage
     * @return
     */
    @PostMapping("/post/sendPostMessage")
    public Map<String, Object> recvGoodMessage(@RequestBody Map<String, Object> goodMessage) {
//        System.out.println(goodMessage);
        int goodId = (int) goodMessage.get("goodId");
        Object message = goodMessage.get("message");

        // 转map
        String messageJsonStr = JSON.toJSONString(message);
        Map<String, Object> messageMap = JSON.parseObject(messageJsonStr);
//        System.out.println(messageMap);

        PostMessage msg = new PostMessage();
        msg.setPostId(goodId);

        int no = postService.countGoodMessagesByGoodId(goodId) + 1;
        msg.setNo(no);

        // HashMap<String,Object> 类型存入一个实体,取出实体报错 类型转换异常
        // 用 Integer fatherNo = (Integer) messageMap.get("fatherNo") 会报错
//        Integer fatherNo = new ObjectMapper().convertValue(messageMap.get("fatherNo"), Integer.TYPE);
//        System.out.println(fatherNo);

        int fatherNo = Integer.parseInt(messageMap.get("fatherNo") + "");
        msg.setFatherNo(fatherNo);

        int userId = Integer.parseInt(messageMap.get("userId") + "");
        msg.setUserId(userId);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String formattedDate = formatter.format(date);
        msg.setDate(formattedDate);

        String text = (String) messageMap.get("text");
        msg.setText(text);

        String sendType = (String) messageMap.get("sendType");
        msg.setSendType(sendType);

        int sendToUserId = Integer.parseInt(messageMap.get("sendToUserId") + "");
        msg.setSendToUserId(sendToUserId);

        String sendToUserNickName = (String) messageMap.get("sendToUserNickName");
        msg.setSendToUserNickName(sendToUserNickName);

        String messageChecked = (String) messageMap.get("messageChecked");
        if ("true".equals(messageChecked)) {
            msg.setMessageChecked(true);
        } else {
            msg.setMessageChecked(false);
        }

        int rt = postService.addPostMessage(msg);
        System.out.println(rt);

        goodMessage.put("no", no);

        return goodMessage;
    }

}
